<?php
/**
 * 会员模型
 * @createtime 2015-08-24 20:29
 * @author Halu
 */
namespace Admin\Model;
use Think\Model;
class UserModel extends Model {

	/**
	 * 通过用户ID获取用户信息
	 * @param int $uid 用户ID
	 * @return bool/array
	 */
	public function getMemberInfoById($uid){
		if (empty($uid) || !is_int($uid)) {
			return false;
		}
		return $this->where(array('id'=>$uid))->find();
	}

	/**
	 * 获取会员状态
	 * @param int $uid
	 * @return mixed
	 */
	public function getMemberStatus($uid){
		if (!$uid || !is_numeric($uid)) {
			return false;
		}
		return $this->where(array('id'=>$uid))->getField('status');
	}
	
	/**
	 * 更改会员状态
	 * @param int $uid 会员ID
	 * @param int $status 禁用状态
	 * @return bool
	 */
	public function changeStatus($status,$uid){
		if (!$uid) {
			return false;
		}
		if ($this->where(array('id'=>$uid))->save(array('status'=>$status)) !== false) {
			return true;
		}
		return false;
	}
	
	//获取会员列表
	public function getMemberList($limit=''){
		if(!empty($limit)){
			$result = M('User')->where('status = 1')->limit($limit)->select();	
		}else{
			$result = M('User')->where('status = 1')->select();	
		}
		return $result;
	
	}
	
	//获取会员数量
	public function getNum(){
		$result = M('User')->where('status = 1')->count();
		return $result;
	}
	
	/*
	 *查询审核的会员
	 */
	public function memberCheckList($limit=''){
		if(!empty($limit)){
			$result = M('User')->where('status = 0')->limit($limit)->select();	
		}else{
			$result = M('User')->where('status = 0')->select();	
		}
		return $result;
	}
	//获取审核会员数量
	public function getCheckNum(){
		$result = M('User')->where('status = 0')->count();
		return $result;
	}
	
	//异步审核
	public function passUser($item_id,$status){
		$result = M('User')->where('status = 0 and id = %d',$item_id)->save(array('status'=>$status));
		return $result;
	}
	
	//查询pos列表
	public function getPosNum($item_id){
		$result = M('PosApply')->count();
		return $result;
	}
	
	
	//查询申请列表
	public function pos_list($limit){
		if(!empty($limit)){
			$result = M('PosApply')->limit($limit)->order('id desc')->select();	
		}else{
			$result = M('PosApply')->order('id desc')->select();	
		}
		return $result;
	
	}
	
	//查询pos列表
	public function getPosUserNum(){
		$result = M('UserUpgradeLog')->count();
		return $result;
	}
	
	//提现会员升级列表
	public function pos_user_list($limit){
		if(!empty($limit)){
			$result = M('UserUpgradeLog')->limit($limit)->select();	
		}else{
			$result = M('UserUpgradeLog')->select();	
		}
		return $result;	
	}
	
	/**
	 * 获取会员下线会员
	 * @param int $uid 会员ID
	 * @param int $page 查询页数
	 * @param int $limit 查询条数
	 * @param int $sort 排序 1:正序 -1:反序
	 * @return array 会员ID组
	 */
	public function getDownMember($uid, $order = '', $limit = '')
	{
		if(!$uid || !is_numeric($uid)){
			return false;
		}
		
		$model = M('User_relationship');
		
		if(!empty($order) && is_string($order)){
			$model->order($order);
		}

		if(!empty($limit) && is_string($limit)){
			$model->limit($limit);
		}
		
		$model->field('subordinate_user');
		
		$subordinate_user = $model->where(array('user_id'=>$uid))->select();
	
		if(empty($subordinate_user)){
			return array();
		}
		
		$in = '';
		foreach($subordinate_user as $k=>$v){
			$in .= $v['subordinate_user'] . ',';
		}
		
		$in = rtrim($in,',');
		
		if(empty($in)){
			return array();
		}

		return $this->where(array('id'=>array('in',$in)))->select();
	}

	/**
	 * 获取下级会员数量
	 * @param int $uid 会员ID
	 * @return int 下级会员数量
	 */
	public function getDownMemberNum($uid)
	{
		return M('UserRelationship')->where(array('user_id' => $uid))->count('id');
	}

	/**
	 * 改变会员总代理身份
	 * @param  int $uid 会员ID
	 * @param  int $superAgent 修改值
	 * @return bool     修改是否成功
	 */
	public function changeSuperAgent($uid, $superAgent)
	{
		// 当前状态
		$now = $this->where(array('id' => $uid))->getField('super_agent');
			// dump($now);
			// dump($superAgent);
		if ($superAgent == $now) {
			// 相同状态，不修改，返回成功
			return true;
		}

		$result = $this->where(array('id' => $uid))->save(array('super_agent' => $superAgent));
			// dump($result);
		if ($result !== false) {
			return true;
		}
		return false;
	}

}